package lostland.gmud.exv2.data;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 新物品栏类
 * Created by 12548 on 2015/5/16.
 */
public class Inventory {

	private final static int EQUIPMENT_KINDS = 20;

	private ArrayList<Item> items = new ArrayList<>();


	private int[] equips = new int[EQUIPMENT_KINDS];

	{
		Arrays.fill(equips, -1);
	}


	public boolean have(Item item){
		for(Item itm : items){
			if(itm.equals(item))
				return true;
		}
		return false;
	}

	public boolean have(int itemTemplateId){
		for(Item itm : items){
			if(itm.getId() == itemTemplateId)
				return true;
		}
		return false;
	}
	public boolean equiping(int itemTemplateId){
		for(int i : equips){
			if(i < 0) continue;
			Item itm = items.get(i);
			if(itm.getId() == itemTemplateId)
				return true;
		}
		return false;
	}

	public boolean equiping(Item item){
		for(int i : equips){
			if(i < 0) continue;
			Item itm = items.get(i);
			if(itm.equals(item))
				return true;
		}
		return false;
	}

	public void give(Item item){
		items.add(item);
	}

	public void equip(int pos){
		if(pos < 0 || pos >= items.size() || pos >= EQUIPMENT_KINDS){
			throw new ArrayIndexOutOfBoundsException(pos);
		}
		int equippos = items.get(pos).getSubkind();
		equips[equippos] = pos;
	}

	public void equip(Item item){
		int pos = items.indexOf(item);
		equip(pos);
	}

	public void unEquip(int pos){
		if(pos < 0 || pos >= items.size() || pos >= EQUIPMENT_KINDS){
			throw new ArrayIndexOutOfBoundsException(pos);
		}

		int equippos = items.get(pos).getSubkind();


		equips[equippos] = -1;
	}

	public void unEquip(Item item){
		int pos = items.indexOf(item);
		unEquip(pos);
	}

	public boolean drop(Item item){
		if(!have(item)) return false;
		if(!item.canDrop()) return false;
		if(equiping(item)) return false;
		items.remove(item);

		return true;
	}



}
